/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package procrastinator.table;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import procrastinator.table.ActivityTable.Period;

/**
 *
 * @author alex
 */
public class JActivityTable extends JTable {
    private Color alternativeBackgroundColor;
    
    public JActivityTable() {
        setDefaultRenderer(Date.class, new DateRenderer());
        setDefaultRenderer(Period.class, new PeriodRenderer());
        
        alternativeBackgroundColor = new Color(200, 200, 255);
        getTableHeader().setReorderingAllowed(false);
        
        setShowGrid(false); // I'll draw my own grid
    }
    
    public void updateRenderers(List<String> categoryNames) {
        //getColumn("Category").setCellRenderer(new CategoryRenderer(categoryNames));
        getColumn("Category").setCellEditor(new CategoryRenderer(categoryNames));
    }
    
    
    
    /**
     * Paints empty rows too, after letting the UI delegate do
     * its painting.
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        paintEmptyRows(g);
    }

    /**
     * Paints the backgrounds of the implied empty rows when the
     * table model is insufficient to fill all the visible area
     * available to us. We don't involve cell renderers, because
     * we have no data.
     */
    protected void paintEmptyRows(Graphics g) {
        final int rowCount = getRowCount();
        final Rectangle clip = g.getClipBounds();
        
        if (rowCount * rowHeight < clip.height) {
            for (int i = rowCount; i <= clip.height/rowHeight; ++i) {
                g.setColor(colorForRow(i));
                g.fillRect(clip.x, i * rowHeight, clip.width, rowHeight);
            }
        }
        
        int x = 0;
        Color oldColor = g.getColor();
        g.setColor(Color.black);
        
        for( int i = 0; i < getColumnCount()-1; i++ ) {
            x += getColumn(getColumnName(i)).getWidth();
            g.drawLine(x-1, (int) clip.getMinY(), x-1, (int) clip.getMaxY());
        }
        
        g.setColor(oldColor);
    }

    /**
     * Changes the behavior of a table in a JScrollPane to be more like
     * the behavior of JList, which expands to fill the available space.
     * JTable normally restricts its size to just what's needed by its
     * model.
     */
    @Override
    public boolean getScrollableTracksViewportHeight() {
        if (getParent() instanceof JViewport) {
            JViewport parent = (JViewport) getParent();
            return (parent.getHeight() > getPreferredSize().height);
        }
        return false;
    }

    /**
     * Returns the appropriate background color for the given row.
     */
    protected Color colorForRow(int row) {
        return (row % 2 == 0) ? alternativeBackgroundColor : getBackground();
    }

    /**
     * Shades alternate rows in different colors.
     */
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (isCellSelected(row, column) == false) {
            c.setBackground(colorForRow(row));
            c.setForeground(UIManager.getColor("Table.foreground"));
        } else {
            c.setBackground(UIManager.getColor("Table.selectionBackground"));
            c.setForeground(UIManager.getColor("Table.selectionForeground"));
        }
        return c;
    }
}
